package org.devocean.groupbuying.web.backend;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.devocean.groupbuying.dao.ArticlesLocalizationDAO;
import org.devocean.groupbuying.domain.Article;
import org.devocean.groupbuying.domain.PurchaseItem;
import org.devocean.groupbuying.service.ArticlesService;
import org.devocean.groupbuying.service.DealService;
import org.devocean.groupbuying.utils.RenderMapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
@RequestMapping(value = { "/gb-cpanel/articles" })
public class ArticlesController {

	@Autowired
	private ArticlesService articlesService;

	@Autowired
	private ArticlesLocalizationDAO articlesLocalizationDAO;
	
	@Autowired
	private DealService dealService;

	@RequestMapping(value = "/")
	public Map<String, ? extends Object> list(
			@RequestParam(value = "start", required = false) Integer start,
			@RequestParam(value = "limit", required = false) Integer size) {
		try {

			int sizeNo = size == null ? 10 : size.intValue();
			List<Article> articles = articlesService.getArticlesEntries(start,sizeNo);

			List<Map<String, Object>> result = new ArrayList<Map<String, Object>>();

			for (Article article : articles) {
				Map<String, Object> data = new ConcurrentHashMap<String, Object>();
				data.put("id", article.getArticleID());
				data.put("title", article.getTitle());
				data.put("lang", article.getLang());
				data.put("al_id", article.getArticlesLocalization().getArticlesLocalizationID());
//				data.put("d_id", article.getArticlesLocalization().getDeals()
//						.size() > 0 ? article.getArticlesLocalization()
//						.getDeals().get(0).getId() : 0);
				result.add(data);

			}

			long total = articlesService.countArticles();
			return RenderMapUtils.getMap(result, total);
		} catch (Exception e) {
			return RenderMapUtils
					.getModelMapError("Error retrieving Articles from database.");
		}
	}

	@RequestMapping(value = "/delete/{id}", method = RequestMethod.DELETE)
	public void delete(@PathVariable("id") Integer id) {
		articlesService.removeArticle(id);
	}

	@RequestMapping(value = "/langs/{lang}", method = RequestMethod.GET)
	public Map<String, ? extends Object> listArticlesByLang(
			@PathVariable("lang") String lang) {
		try {

			List<Article> articles = articlesService.getArticlesByLang(lang);
			List<Map<String, Object>> result = new ArrayList<Map<String, Object>>();

			for (Article article : articles) {
				Map<String, Object> data = new ConcurrentHashMap<String, Object>();
				data.put("title", article.getTitle());
				data.put("al_id", article.getArticlesLocalization().getArticlesLocalizationID());
				result.add(data);

			}
			return RenderMapUtils.getMap(result, result.size());
		} catch (Exception e) {
			return RenderMapUtils
					.getModelMapError("Error retrieving Articles from database.");
		}
	}
	
	@RequestMapping(value = "/{id}/purchases", method = RequestMethod.GET)
	public Map<String, ? extends Object> getPurchases(
			@PathVariable("id") Integer id) {
		try {
			
			List<PurchaseItem> purchases =null;// dealService.getDealById(id).getPurchaseItems();
			

			List<Map<String, Object>> result = new ArrayList<Map<String, Object>>();

			for (PurchaseItem purchaseItem : purchases) {
				Map<String, Object> data = new ConcurrentHashMap<String, Object>();
				data.put("id", purchaseItem.getPurchaseItemID());
				data.put("unitPrice", purchaseItem.getUnitPrice());
				data.put("amount", purchaseItem.getAmount());
				data.put("quantity", purchaseItem.getQuantity());
				data.put("statusDate", purchaseItem.getStatusDate().toString());
				data.put("status", purchaseItem.getStatus());
				result.add(data);
			}
			return RenderMapUtils.getMap(result, result.size());
		} catch (Exception e) {
			return RenderMapUtils
					.getModelMapError("Error retrieving Articles from database.");
		}
	}

	

}
